package com.gds.wiki.cloud.admin.server.modular.sys.controller;

import com.gds.wiki.cloud.admin.client.sys.model.Role;
import com.gds.wiki.cloud.admin.client.sys.model.query.RoleQuery;
import com.gds.wiki.cloud.admin.server.modular.sys.service.DeptService;
import com.gds.wiki.cloud.admin.server.modular.sys.service.RoleService;
import com.gds.wiki.cloud.framework.common.dto.ResponseDto;
import com.gds.wiki.cloud.framework.common.exception.BizExceptionEnum;
import com.gds.wiki.cloud.framework.web.common.BaseController;
import com.gds.wiki.cloud.oauth2.admin.utils.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.List;

@Api(value = "RoleController",tags = "角色控制器")
@RestController
@RequestMapping("role")
@Validated
public class RoleController extends BaseController {


    @Autowired
    private RoleService roleService;

    @Autowired
    private DeptService deptService;

    @PostMapping
    @ApiOperation(value = "角色列表", notes = "角色列表", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "请输入凭证", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "roleQuery", value = "请输入权限信息", required = true, dataType = "RoleQuery", paramType = "body") })

    public ResponseDto<Role> roleList(@RequestBody @Validated RoleQuery roleQuery
    ) throws Exception {
             ResponseDto rs=new ResponseDto<Role>(BizExceptionEnum.SUCCESSFUL_OPERATION);

        Long deptId=SecurityUtil.getSecurityUser().getDeptId();
        if(roleQuery.getDeptId()==null){
            roleQuery.setDeptId(deptId);
        }else {
            if(deptId!=0){
                List<Long> list=deptService.deptIdsByPid(deptId);
                if(!list.contains(roleQuery.getDeptId())){
                    rs.setCode(BizExceptionEnum.FAIL_OPERATION.getCode());
                    rs.setMesg("查看部门权限不足");
                    return  rs;
                }
            }
        }
            rs.setData(roleService.selectPage(roleQuery));
            return rs;
    }

    @PostMapping("save")
    @ApiOperation(value = "角色添加或修改", notes = "角色添加或修改", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "请输入凭证", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "role", value = "请输入角色信息", required = true, dataType = "Role", paramType = "body") })
    public ResponseDto saveOrUpdate(@RequestBody @Validated Role role){
        ResponseDto rs=new ResponseDto(BizExceptionEnum.SUCCESSFUL_OPERATION);
        roleService.saveOrUpdate(role);
        return rs;
    }

    @PostMapping("auth_resource")
    @ApiOperation(value = "角色授权菜单资源", notes = "角色授权菜单资源", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "请输入凭证", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "roleId", value = "请输入角色id", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "resourceIds", value = "请输入菜单资源ids", required = true, dataType = "String", paramType = "query")})

    public ResponseDto authRoleResource(@NotNull(message = "roleId不能为空") Long roleId, @NotNull(message = "resourceIds不能为空") String resourceIds){
        ResponseDto rs=new ResponseDto(BizExceptionEnum.SUCCESSFUL_OPERATION);
        String[] ids=resourceIds.split(",");
        roleService.AuthRoleResource(roleId,ids);
        return rs;
    }

    @PostMapping("role_resources")
    @ApiOperation(value = "查询角色相关资源", notes = "查询角色相关资源", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "请输入凭证", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "roleId", value = "请输入角色id", required = true, dataType = "Long", paramType = "query"),
    })

    public ResponseDto getRoleResources(@NotNull(message = "roleId不能为空") Long roleId){
        ResponseDto rs=new ResponseDto(BizExceptionEnum.SUCCESSFUL_OPERATION);
        rs.setData(roleService.roleResources(roleId));
        return rs;
    }

    @PostMapping("role_select")
    @ApiOperation(value = "角色selecti框", notes = "角色selecti框", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "请输入凭证", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "deptId", value = "请角色部门id",dataType = "Long", paramType = "query"),
    })
    public ResponseDto RoleSelection(Long deptId){
        ResponseDto rs=new ResponseDto(BizExceptionEnum.SUCCESSFUL_OPERATION);
        rs.setData(roleService.roleSelect(deptId));
        return rs;
    }


}
